package com.link.base.base.productplan.controller;

import com.link.base.base.productplan.model.ProdPlan;
import com.link.base.base.productplan.service.ProdPlanService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.query.Sorter;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments 产品计划信息
 */
@Controller
@RequestMapping("/link/prodPlan")
public class ProdPlanController extends BasicController<ProdPlan> {

    @Resource
    private ProdPlanService prodPlanService;

    @Override
    public BasicService<ProdPlan> getBasicService() throws Exception {
        return prodPlanService;
    }

    /**
     * 根据HEAD_ID查询数据
     */
    @RequestMapping(value = "/queryByHeadId")
    @ResponseBody
    public Map<String, Object> queryByHeadId(@JsonParam QueryParams qps, @JsonParam ProdPlan entity, HttpSession session,
                                             HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            List<ProdPlan> list = null;
            qps.preDealSecurity(request);
            /*String id = null;
            if (qps.getFiltersRaw() != null) {
                String jsonStr = qps.getFiltersRaw();
                jsonStr = jsonStr.replace("[", "");
                jsonStr = jsonStr.replace("]", "");
                JSONObject jsonObject = JSONObject.fromObject(jsonStr);

                id = jsonObject.getString("value");
            }*/
            ProdPlan t = (ProdPlan) BasicModel.transformClass(entity, qps);
            if (qps.getId() != null) {
                Long id = qps.getId();
                list = prodPlanService.queryByHeadId(id);
            } else {
                //List<Filter> filterList = qps.getFilterByProperty("head_id");
                List<Sorter> sorterList = qps.getSorterByProperty("seq");
                qps.setSorters(sorterList);
                //List<ProdPlan> list = prodPlanService.queryByHeadId(id);
                list = getBasicService().queryByExamplePage(t);
            }

            result.put("success", true);
            result.put("rows", list);
            this.pushCurrentDataCache(list, session.getId(), "base", false);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<ProdPlan>());
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<ProdPlan>());
            result.put("result", e.getMessage());
        }
        return result;
    }

}
